﻿@model Citi.EmailSender.WebUi.Models.EmailDataModel
@{
    ViewBag.Title = "Create Template";
}

<script type="text/javascript">
    function removeTr(guid) {
        $("#" + guid).remove();

        if ($('#tableParameters tr').length == 2)
            $('#tableParameters tr:last').show();
    }

    $(function () {
        $("#templateForm").submit(function (e) {
            var self = this;
            e.preventDefault();

            if ($("#Subject").val() == '') {
                $("#Subject").focus();
                alert('Subject can not be empty');
                return false;
            }
            var selected = $('input[name=bodyContent]:checked')[0].id;
            if (selected == 'writeHtml' && $("#Body").val() == '') {
                $("#Body").focus();
                alert('Body can not be empty');
                return false;
            }
            else if (selected == 'url' && $("#Url").val() == '') {
                $("#Url").focus();
                alert('Url can not be empty');
                return false;
            }
            else if (selected == 'uploadPage' && $("#uploadHtml").val() == '') {
                $("#uploadHtml").focus();
                alert('No file upload');
                return false;
            }

            $('#tableParameters td:first-child').each(function (index, item) {
                if (item.innerText == 'No parameters ') return;

                $("#Parameters").val($("#Parameters").val() + item.innerText + ';');
            });

            self.submit();
        });

        $("#Body").jqte();

        $("input[name='bodyContent']").change(function () {
            $(".divBody").hide();
            var selected = $('input[name=bodyContent]:checked')[0].id;
            if (selected == 'writeHtml')
                $("#emailBody").show();
            else if (selected == 'url')
                $("#emailUrl").show();
            else
                $("#emailUpload").show();
        });

        $("#addParameter").click(function () {
            var parameterName = $("#parameterName").val();

            if (parameterName == '') return;

            $.guid = 0;
            var guid = "'" + ($.guid++) + "'";

            if ($('#tableParameters tr').length == 2)
                $('#tableParameters tr:last').hide();

            $('#tableParameters tr:last').after('<tr id=' + guid + '><td>' + parameterName + '</td><td><a href="#" class="icon-remove" title="Delete" onClick="removeTr(' + guid + ')"></a></td></tr>');

            $("#parameterName").val('');
            $("#parameterName").focus();
        });
    });
</script>
<h3>
    Create Template</h3>
<hr />
@using (Html.BeginForm("Create", "Template", FormMethod.Post, new { id = "templateForm", enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <div class="editor-field">
            Application:&nbsp; @Html.DropDownListFor(model => model.ApplicationId, (SelectList)ViewBag.Applications)
            @Html.ValidationMessageFor(model => model.ApplicationId)
            &nbsp;&nbsp;&nbsp;On Behalf Of:&nbsp; @Html.DropDownListFor(model => model.OnBehalfOfId, (SelectList)ViewBag.OnBehalfOfs)
            @Html.ValidationMessageFor(model => model.OnBehalfOfId)
            &nbsp;&nbsp;&nbsp;Importance:&nbsp; @Html.DropDownListFor(model => model.Importance, (List<SelectListItem>)ViewBag.Importances)
            @Html.ValidationMessageFor(model => model.Importance)
        </div>
        <br />
        <div class="editor-field">
            Subject: @Html.EditorFor(model => model.Subject)
            @Html.ValidationMessageFor(model => model.Subject)
        </div>
        <div style="border: 1px solid #d3d3d3; padding: 7px; margin-top: 10px;">
            <h4>
                Email body</h4>
            <div style="line-height: 25px; vertical-align: middle;">
                @Html.RadioButton("bodyContent", "writeHtml", true, new { id = "writeHtml" }) Write
                HTML
                @Html.RadioButton("bodyContent", "url", new { id = "url" }) URL
                @Html.RadioButton("bodyContent", "uploadPage", new { id = "uploadPage" }) Upload
                a HTML Page
            </div>
            <div id="emailUpload" style="display: none; margin-top: 10px;" class="divBody">
                Upload file:
                <input type="file" id="uploadHtml" name="uploadHtml" />
            </div>
            <div id="emailUrl" style="display: none; margin-top: 10px;" class="divBody">
                Page Url: @Html.EditorFor(model => model.Url)
                @Html.ValidationMessageFor(model => model.Url)
            </div>
            <div id="emailBody" class="divBody">
                @Html.TextAreaFor(model => model.Body)
                @Html.ValidationMessageFor(model => model.Body)
            </div>
        </div>
        <div style="border: 1px solid #d3d3d3; padding: 7px; margin-top: 10px;">
            <h4>
                Parameters</h4>
            Name:
            <input type="text" id="parameterName" />&nbsp;&nbsp;
            <input type="button" value="Add" id="addParameter" />
            <br /><br />
            <table id="tableParameters" style="width: 40%;" class="table table-bordered table-striped">
                <tr>
                    <th>
                        Name
                    </th>
                    <th>
                    </th>
                </tr>
                <tr>
                    <td colspan="2">
                        No parameters
                    </td>
                </tr>
            </table>
        </div>
        @Html.Hidden("Parameters")
        <br />
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>
